<template>
  <form action="/">
    <van-search
        v-model="searchText"
        show-action
        placeholder="请输入标签"
        @search="onSearch"
        @cancel="onCancel"
    />
  </form>

  <van-divider content-position="left">已选择标签</van-divider>
  <div v-if="activeIds.length === 0">请选择标签</div>
  <van-space wrap>
    <van-tag v-for="tag in activeIds" closeable size="small" type="primary" @close="doclose(tag)">
      {{ tag }}
    </van-tag>
  </van-space>

  <van-divider content-position="left">选择标签</van-divider>

  <van-tree-select
      v-model:active-id="activeIds"
      v-model:main-active-index="activeIndex"
      :items="tarList"
  />

  <div style="margin: 12px">
    <van-button block type="primary" @click="doSearchResult()">搜索</van-button>
  </div>

</template>

<script setup lang="ts">
import {ref} from "vue";
import { useRouter} from "vue-router";

const searchText = ref('');

const originTarList = [
  {
    text: '性别',
    children: [
      { text: '男', id: '男' },
      { text: '女', id: '女' },
    ],
  },
  {
    text: '年级',
    children: [
      { text: '大1', id: '大1' },
      { text: '大2', id: '大2' },
      { text: '大3', id: '大3' },
      { text: '大4', id: '大4' },
      { text: '大55555555555', id: '大55555555555' },
      { text: '大66666666666666666', id: '大66666666666666666' },
    ],
  },
];

let tarList = ref(originTarList);

const onSearch = (val: string) => {
  tarList.value = originTarList.map(parentTag => {
    const tempParentTag = {...parentTag};
    const tempChildren = [...parentTag.children];
    tempParentTag.children = tempChildren.filter(item => item.text.includes(val))
    return tempParentTag;
  })
};
const onCancel = () => {
  activeIds.value = [];
  tarList.value = originTarList;
};

const activeIds = ref([]);
const activeIndex = ref(0);

const doclose = (tag: string) => {
  activeIds.value = activeIds.value.filter(item => {
    return item !== tag;
  })
}

const router = useRouter();
const doSearchResult = () => {
  router.push({
    path: '/user/list',
    query :{
      tags: activeIds.value
    }
  });
}

</script>

<style scoped>

</style>